﻿using System.IO;
using System.Text;

/// <summary>
/// 文件工具
/// </summary>
public static class IOUtil
{
    #region 读取文件
    /// <summary>
    /// 读取所有文本数据
    /// </summary>
    /// <param name="filePath"></param>
    /// <returns></returns>
    public static byte[] ReadBuffer(string filePath)
    {
        if (!ExistsFile(filePath)) return null;
        byte[] buffer = null;
        using (FileStream fs = File.Open(filePath, FileMode.Open))
        {
            buffer = new byte[fs.Length];
            fs.Read(buffer, 0, buffer.Length);
        }
        return buffer;
    }

    /// <summary>
    /// 读取所有文本数据
    /// </summary>
    /// <param name="filePath"></param>
    /// <returns></returns>
    public static string ReadAllFile(string filePath)
    {
        if (!ExistsFile(filePath)) return null;
        return File.ReadAllText(filePath);
    }

    /// <summary>
    /// 读取所有文本数据
    /// </summary>
    /// <param name="filePath"></param>
    /// <returns></returns>
    public static string ReadAllFile2(string filePath)
    {
        if (!ExistsFile(filePath)) return null;
        string strConcent = null;
        using (StreamReader sr = File.OpenText(filePath))
        {
            strConcent = sr.ReadToEnd();
        }
        return strConcent;
    }

    /// <summary>
    /// 逐行解析文本
    /// </summary>
    /// <param name="fileContent"></param>
    /// <param name="handle"></param>
    public static void ReadFileContent(string fileContent, System.Action<string> handle)
    {
        using (StringReader resder = new StringReader(fileContent))
        {
            string line = string.Empty;
            while ((line = resder.ReadLine()) != null)
            {
                handle(line);
            }
        }
    }

    /// <summary>
    /// 读取文件及解析委托
    /// </summary>
    /// <param name="fileContent"></param>
    /// <param name="handle"></param>
    public static void ReadAllFile(string filePath, System.Action<string> handle)
    {
        string fileContent = ReadAllFile(filePath);
        AppDeBug.Log(fileContent);
        ReadFileContent(fileContent, handle);
    }
    #endregion
     
    #region 文件 写入

    /// <summary>
    /// 创建写入文本
    /// </summary>
    /// <param name="filePath"></param>
    /// <param name="value"></param>
    public static void CreateFile(string filePath, string value)
    {
        if ( string.IsNullOrEmpty(filePath)) 
            return; 
        CreateFolder(filePath);

        if (File.Exists(filePath))
        {
            File.Delete(filePath);
        }

        using (StreamWriter sw = File.CreateText(filePath))
        {
            sw.Write(value);
        }
    }

    /// <summary>
    /// 创建写入文本
    /// </summary>
    /// <param name="filePath"></param>
    /// <param name="bytes"></param>
    public static void CreateFile(string filePath, byte[] bytes)
    {
        if (bytes.IsNull() || string.IsNullOrEmpty(filePath)) 
            return;
        

        CreateFolder(filePath);

        using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
        {
            fs.Write(bytes, 0, bytes.Length);
            fs.Flush();
            fs.Close();
            fs.Dispose();
        } 

        //using (FileStream fs = File.Create(filePath, bytes.Length))
        //{
        //    fs.Write(bytes, 0, bytes.Length);
        //    fs.Close();
        //}
    }

    /// <summary>
    /// 添加数据
    /// </summary>
    /// <param name="_path">文件夹路径</param>
    /// <param name="_name">文件名称</param>
    /// <param name="_data">数据</param>
    public static void Append(string _path, string _name, string _data)
    {
        StreamWriter sw = File.AppendText(_path + _name);
        sw.WriteLine(_data);
        sw.Close();
    }


    /// <summary>
    /// 拷贝文件夹
    /// </summary>
    /// <param name="sourcesFolderPath">源文件路径</param>
    /// <param name="destFolderPath">目标文件路径</param>
    public static void CopyFolder(string sourcesFolderPath, string destFolderPath)
    {
        if (!Directory.Exists(sourcesFolderPath))
            return;
        if (!Directory.Exists(destFolderPath))
        {
            Directory.CreateDirectory(destFolderPath);
            File.SetAttributes(destFolderPath, File.GetAttributes(sourcesFolderPath));
        }

        if (destFolderPath[destFolderPath.Length - 1] != Path.DirectorySeparatorChar)
        {
            destFolderPath = destFolderPath + Path.DirectorySeparatorChar;

            //文件
            string destFilePath = null;
            string[] files = Directory.GetFiles(sourcesFolderPath);
            foreach (string file in files)
            {
                destFilePath = destFolderPath + Path.GetFileName(file);
                //if (File.Exists(destFilePath))
                //    continue;
                FileInfo fileInfo = new FileInfo(file);
                if (fileInfo.Extension.Equals(".meta", System.StringComparison.CurrentCultureIgnoreCase))
                    continue;
                File.Copy(file, destFilePath, true);
                File.SetAttributes(destFilePath, FileAttributes.Normal);
            }

            //文件夹
            string[] folders = Directory.GetDirectories(sourcesFolderPath);
            foreach (var folder in folders)
            {
                CopyFolder(folder, destFolderPath + Path.GetFileName(folder));
            }
        }
    }

    #endregion  

    #region 文件是否存在、创建、删除

    /// <summary>
    /// 文件是否存在
    /// </summary>
    /// <param name="filePath"></param>
    /// <returns></returns>
    public static bool ExistsFile(string filePath)
    {
        return File.Exists(filePath);
    }

    /// <summary>
    /// 删除文件
    /// </summary>
    /// <param name="filePath"></param>
    public static void DeleteFile(string filePath)
    {
        if (ExistsFile(filePath))
            File.Delete(filePath);
    }

    /// <summary>
    /// 文件夹是否存在
    /// </summary>
    /// <param name="folderPath"></param>
    /// <returns></returns>
    public static bool ExistsFolder(string folderPath)
    {
        return Directory.Exists(folderPath);
    }

    /// <summary>
    /// 删除文件夹
    /// </summary>
    /// <param name="folderPath"></param>
    public static void DeleteFolder(string folderPath)
    {
        if (ExistsFolder(folderPath))
            Directory.Delete(folderPath, true);
    }

    /// <summary>
    /// 创建文件夹
    /// </summary>
    /// <param name="folderPath"></param>
    public static void CreateFolder(string folderPath)
    {
        string path = GetFolderPath(folderPath);
        if (!ExistsFolder(path))
            Directory.CreateDirectory(path);
    }

    public static string GetFolderPath(string folderPath)
    {
        string path = folderPath;
        int index = folderPath.LastIndexOf('.');
        if (index != -1)
        {
            index = folderPath.LastIndexOf('/');
            if (index != -1)
            {
                path = folderPath.Substring(0, index);
            }
        }

        return path;
    }



    #endregion

}
